//#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define LEN sizeof(Student)
#define Room_num 500

typedef struct Student //确认结构体
{
	long long int Id;  //学号
	char Name[10];    //姓名
	char Sex[10];    //性别
	char Date[10];    //出生日期
	float egrade[10];        //专业
}Student;

void Title();//标题函数

void Menu();//菜单函数

void WriteToFile();//信息输入的函数

void ReadFromFile();//显示所有信息的函数

void QueryFile();//查询函数

void ModifyFile();//修改函数

void DeletFile();//删除数据的函数


int main()
{
	system("color E0");
	int select;

	do
	{
		system("cls");
		Title();
		Menu();
		scanf_s("%d", &select);
		switch (select)
		{
		case 1:
			WriteToFile();
			break;

		case 2:
			ReadFromFile();
			break;

		case 3:
			QueryFile();
			break;

		case 4:
			ModifyFile();
			break;

		case 5:
			DeletFile();
			break;

		default:
			printf("退出程序！");
			exit(0);
			break;
		}
	} while ((select == 1 || select == 2) || (select == 3 || select == 4) || (select == 5));

	return 0;
}//利用switch函数进行菜单的选择

void Title()
{
	//int num = Left();
	printf("**********欢迎使用学籍管理系统***********\n");


}//标题函数1

void Menu()
{
	printf("********************系统功能菜单********************\n");
	printf("*                                                  *\n");
	printf("*                 1.录入学生信息                   *\n");
	printf("*                 2.显示所有信息                   *\n");
	printf("*                 3.查询学生信息                   *\n");
	printf("*                 4.修改学生信息                   *\n");
	printf("*                 5.删除学生信息                   *\n");
	printf("*                 0.退出系统                       *\n");
	printf("*                                                  *\n");
	printf("———————————————————————--------------------—------——\n\n");
	printf("请选择菜单编号:");

}//标题函数2

void WriteToFile()
{
	FILE* fp = NULL;
	Student stu = { 0 };
	char flag = 'y';
	fp = fopen_s("&fp","D:\\code2\\ARS\\ARS.txt", "w+");//打开文件
	if (fp == NULL)
	{
		printf("文件打开失败！\n");
		exit(1);//1表示在有错的方式退出程序
	}

	while ((flag == 'y' || flag == 'Y'))
	{
		system("cls");
		Title();

		printf("请输入证件号id：");
		scanf_s("%lld", &stu.Id);

		printf("请输入姓名：");
		scanf_s("%s", &stu.Name);

		printf("请输入性别：");
		scanf_s("%s", &stu.Sex);

		printf("请输入籍贯：");
		scanf_s("%s", &stu.Date);

		printf("请输入专业：");
		scanf_s("%f", &stu.egrade);


		fwrite(&stu, LEN, 1, fp);
		fflush(stdin);

		printf("继续输入吗？继续请输入y或Y，输入其他则退出：");
		getchar();
		scanf_s("%c", &flag);
	}

	fclose_s(fp);//关闭文件
	return;
}//添加函数

void ReadFromFile()
{
	system("cls");
	Title();
	FILE* fp = NULL;
	Student stu;
	fp = fopen_s("&fp","D:\\code2\\ARS\\ARS.txt", "w+");
	if (fp == NULL)
	{
		printf("文件打开失败");
		exit(1);
	}
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\tid\t姓名\t性别\t籍贯\t专业\n");

	fseek(fp, 0, SEEK_SET);

	while (!feof(fp))
	{
		if (fread(&stu, LEN, 1, fp))
		{
			printf("%lld\t %s\t %s\t %s\t %f\n", stu.Id, stu.Name, stu.Sex, stu.Date, stu.egrade);
		}
	}
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	system("pause");
	system("cls");
	fclose_s(fp);
	return;
}

void QueryFile()
{
	system("cls");
	Title();
	Student stu;
	char x[8];
	int flag = 0;
	FILE* fp;
	long long int num;
	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("\t\t请输入学生姓名:");
	scanf_s("%s", x);
	printf("\tid\t姓名\t性别\t籍贯\t专业\n");


	fp = fopen_s("&fp","D:\\code2\\ARS\\ARS.txt", "rb");

	if (fp == NULL)
	{
		printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		printf("错误\n");
		printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		return;
	}

	fseek(fp, 0, SEEK_SET);
	while (fread(&stu, LEN, 1, fp))
	{

		if (strcmp(x, stu.Name) == 0)
		{
			printf("%lld\t %s\t %s\t %s\t %f\n", stu.Id, stu.Name, stu.Sex, stu.Date, stu.egrade);
			flag = 1;
		}

		if (flag = 0)
		{
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			printf("查无此人");
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		}
	}

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	fclose_s(fp);
	system("pause");
	system("cls");
	return;
}

void ModifyFile()
{
	system("cls");
	Title();
	Student stu;
	FILE* fp;
	char x[8];

	printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
	printf("请输入学生姓名:");
	scanf_s("%s", x);

	fp = fopen_s("fp","D:\\code2\\ARS\\ARS.txt", "rb+");

	if (fp == NULL)
	{
		printf("文件打开失败");
		exit(1);
	}

	fseek(fp, 0, SEEK_SET);
	while (fread(&stu, LEN, 1, fp))
	{

		if (strcmp(x, stu.Name) == 0)
		{
			printf("请输入证件号id：");
			scanf_s("%lld", &stu.Id);

			printf("请输入姓名：");
			scanf_s("%s", &stu.Name);

			printf("请输入性别：");
			scanf_s("%s", &stu.Sex);

			printf("请输入籍贯：");
			scanf_s("%s", &stu.Date);

			printf("请输入专业：");
			scanf_s("%f", &stu.egrade);
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			fflush(stdin);
			fseek(fp, 0 - LEN, SEEK_CUR);
			fwrite(&stu, LEN, 1, fp);
			fclose_s(fp);
		}

		if (feof(fp))
		{
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
			printf("没有该学生信息");
			printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
		}

	}

	system("pause");
	system("cls");
	return;
}

void DeletFile()
{
	system("cls");
	Title();
	Student s;
	FILE* fp;
	fp = fopen_s("&fp","D:\\code2\\ARS\\ARS.txt", "rb+");
	if (fp == NULL)
	{
		printf("打开文件错误！！！\n");
		exit(1);
	}

	printf("\n请输入学生ID：");
	long long int num;
	scanf_s("%lld", &num);
	printf("\n\t\t\t删除成功\n");
	fseek(fp, 0, SEEK_SET);
	FILE* fp1;
	fp1 = fopen_s("&fp","D:\\code2\\ARS\\ARS.txt", "ab+");//读写新建一个临时文件
	while (fread(&s, LEN, 1, fp))//从原文件读一个结点
	{
		if (num != s.Id)//不是要删除的内容
		{
			fwrite(&s, LEN, 1, fp1);
		}
	}

	fclose_s(fp);
	fclose_s(fp1);
	remove("D:\\code2\\ARS\\ARS.txt");//删除原文件
	rename("linshi.dat", "D:\\code2\\ARS\\ARS.txt");//重命名为原文件

	fflush(stdin);
	system("pause");
	system("cls");
	return;
}